package com.tuojiahr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tuojiahr.common.HibernatePageCommon;
import com.tuojiahr.domain.TuoJiaHrEmployee;
import com.tuojiahr.domain.TuoJiaHrUser;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.EmployeeService;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-12 下午11:57:09
 */

@Transactional
public class EmployeeServiceImpl extends HibernatePageCommon implements
		EmployeeService {

	//在线报名
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void addEmployee(TuoJiaHrEmployee employee) {
		employee.setAssign("n");
		employee.setIdelete("n");
		Date now = new Date();
		employee.setCreateTime(now);
		employee.setUpdateTime(now);
		getHibernateTemplate().persist(employee);
	}

	//更新报名人员的分配状态
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updateEmployee(String assign, long id, TuoJiaHrUser user) {
		getSession().createQuery("update TuoJiaHrEmployee e set e.assign = :assign, e.operator= :user where e.id = :id")
			.setParameter("assign", assign)
			.setParameter("user", user)
			.setParameter("id", id)
			.executeUpdate();
	}

	//彻底删除，支持批量
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteTrueEmployee(String ids) {
		if(StringUtil.isNotBlank(ids)) {
			List<Long> idList = new ArrayList<Long>();;
			String[] idArr = ids.split("\\|");
			for (String id : idArr) {
				idList.add(Long.valueOf(id));
			}
			getSession().createQuery("delete from TuoJiaHrEmployee e where e.id in (:idList)")
				.setParameterList("idList", idList)
				.executeUpdate();
		}
	}

	//更新报名人员为已删除，支持批量
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteEmployee(String ids, TuoJiaHrUser user) {
		if(StringUtil.isNotBlank(ids)) {
			List<Long> idList = new ArrayList<Long>();;
			String[] idArr = ids.split("\\|");
			for (String id : idArr) {
				idList.add(Long.valueOf(id));
			}
			getSession().createQuery("update TuoJiaHrEmployee e set e.idelete = 'y', e.operator = :user where e.id in (:idList)")
				.setParameter("user", user)
				.setParameterList("idList", idList)
				.executeUpdate();
		}
	}

	//根据手机号判断是否已经报名
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public String checkMobile(String mobile) {
		String result = "y";
		if(StringUtil.isNotBlank(mobile)) {
			int count =  ((Long) getSession().createQuery("select count(*) from TuoJiaHrEmployee e where e.mobile = :mobile and e.idelete = 'n'")
					.setParameter("mobile", mobile).uniqueResult())
					.intValue();
			if(count <= 0) {
				result = "n";
			}
		}
		return result;
	}

	//根据手机号向后模糊、分配状态，查询未删除的报名人员，分页
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public PageModel getEmployeeNoDeleteList(String keyWord, String assign) {
		if(StringUtil.isNotBlank(keyWord)) {
			Object[] params = new Object[]{keyWord, assign};
			return this.searchPaginated("from TuoJiaHrEmployee e where e.mobile like ? and e.assign = ? and e.idelete = 'n'", params);
		}
		return this.searchPaginated("from TuoJiaHrEmployee e where e.assign = ? and e.idelete = 'n'", assign);
	}

	//根据手机号向后模糊，查询已删除的报名人员，分页
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public PageModel getEmployeeIsDeleteList(String keyWord) {
		if(StringUtil.isNotBlank(keyWord)) {
			return this.searchPaginated("from TuoJiaHrEmployee e where e.mobile like ? and e.idelete = 'y'", keyWord);
		}
		return this.searchPaginated("from TuoJiaHrEmployee e where e.idelete = 'y'");
	}

}
